<?
require_once('functions.php');

function GetSources()
{
	$connect = GetConnection();
	return $connect->query('SELECT * FROM Psi_Sources');
}
function GetSource($id)
{
	$connect = GetConnection();
	$sql = "SELECT * FROM Psi_Sources WHERE id=$id";
	$results = $connect->query($sql);
	//echo $sql; die();
	$row = $results->fetch_assoc();
	$connect->close();
	return $row;
}
function BlankSource()
{
	return array('Title'=>null,'Author'=>null,'PublishYear'=>null,'Cite'=>null,'created_at'=>null,'updated_at'=>null,'id'=>null);
}
function SaveSource($row)
{
	$connect = GetConnection();
	$escape = EscapeRow($row, $connect);
	$sql = 	"UPDATE Psi_Sources "
		.	"Set"
		
		.	" Title='$escape[Title]', "
		.	" Author='$escape[Author]', "
		.	" PublishYear='$escape[PublishYear]', "
		.	" Cite='$escape[Cite]' "
								
		.	"WHERE id=$row[id] ";
	$results = $connect->query($sql);
	//echo $sql;
	$error = $connect->error;
	$connect->close();
	
	return $error == '' ? TRUE : array('SQL Error' => $error);
}
function CreateSource($row) {
	$connect = GetConnection();
	$escape = EscapeRow($row, $connect);
	
	$Title 			= 	$_POST['Title'];
	$Author 		= 	$_POST['Author'];
	$PublishYear 	= 	$_POST['PublishYear'];
	$Cite 			= 	$_POST['Cite'];
	
	$sql = 
		"INSERT INTO Psi_Sources (
			Title, Author, PublishYear, Cite, created_at
		) VALUES (
			'$escape[Title]', '$escape[Author]', '$escape[PublishYear]', '$escape[Cite]', NOW()
		)";
	
	$result = $connect->query($sql);
	if ($result) {
		// Success!
		header("Location: index.php");
		exit;
	} else {
		// Display error message.
		echo "<p class=\"error\">Subject creation failed.</p>";
		echo "<p class=\"error\">" . mysql_error() . "</p>";
	}

	$connect->close();
}
function DeleteSource($id)
{
	$conn = GetConnection();
	$sql = 	"DELETE FROM Psi_Sources WHERE id=$id";
	$results = $conn->query($sql);
	//echo $sql;
	$error = $conn->error;
	$conn->close();
	
	return $error == '' ? true : array('SQL Error' => $error);
}

/*function DeleteSource($row) {
	$connect = GetConnection();

	if (intval($_GET['id']) == 0) {
			header("Location: index.php");
			die();
	}
	
	$id = mysql_prep($_GET['id']);
	
	if ($subject = GetSource($id)) {
		
		$sql = "DELETE FROM Psi_Sources WHERE id = {$id} LIMIT 1";
		$result = $connect->query($sql);
		if ( $connect->affected_rows == 1 ) {
			header("Location: index.php");
			die();
		} else {
			// Deletion Failed
			echo "<p class=\"error\">Subject deletion failed.</p>";
			echo "<p class=\"error\">" . mysql_error() . "</p>";
			echo "<a href=\"index.php\">Return to Main Page</a>";
		}
	} else {
		// Theorist didn't exist in database
			header("Location: index.php");
			die();
	}

	$connect->close();
}
*/
function ValidateSource($row)
{
	$errors = array();
	if(empty($row['Title'])) $errors['Title'] = 'Title is required';
	if(empty($row['Author'])) $errors['Author'] = 'Author is required';
	if(empty($row['PublishYear'])) $errors['PublishYear'] = 'Publish Year is required';
	if(empty($row['Cite'])) $errors['Cite'] = 'Citation is required in APA Formatting.  For more information on this, visit <a href="http://owl.english.purdue.edu/owl/resource/560/01/">Owl Perdue</a>.';
	
	return count($errors) == 0 ? true : $errors;
}